<?php
defined('_JEXEC') or die('Restricted access');
 
class DoskaViewCategories extends JViewLegacy {
	
	
	protected $items;
	protected $pagination;
	protected $state;
	
	protected $listOrder;
	protected $listDirn;
	protected $saveOrder;
	protected $categories;
	
	
	
	public function display($tpl = null) 
	{
		
		$this->sidebar = DoskaHelper::addSubmenu('categories');
		
		$categories = $this->get('Items');//getItems();
		$this->categories = $categories;//getItems();
		
		$this->items = array();
		
		if(is_array($categories)) {
			foreach($categories as $row) {
			
			if(!$row->parentid) {
				$this->items[$row->id]['name'] = $row->name;
				$this->items[$row->id]['state'] = $row->state;
				$this->items[$row->id]['ordering'] = $row->ordering;
			}
			else {
				$this->items[$row->parentid]['next'][] = array(
													'id'=>$row->id,
													'name'=>$row->name,
													'state'=>$row->state,
													'ordering'=>$row->ordering
													);
			}
		}
		}
		
		
		$this->pagination = $this->get('Pagination');//getPagination
		$this->state = $this->get('State');//getState
		
		
		
		$this->listOrder = $this->escape($this->state->get('list.ordering'));
		$this->listDirn = $this->escape($this->state->get('list.direction'));
		
		$this->saveOrder =  $this->listOrder == 'ordering';
		
		/*echo '<pre>';
		print_r($this->state);
		echo '</pre>';*/
		
		$this->addToolBar();
		$this->setDocument();
		
		parent::display($tpl);
	}
	
	
	protected function addToolBar() {
		
		JToolBarHelper::title(JText::_('COM_DOSKA_MANAGER_CATEGORIES'));
		
		JToolBarHelper::addNew('category.add',JText::_('COM_DOSKA_MANEGER_CATEGORIES_ADD'));	
		JToolBarHelper::editList('category.edit');
		
		JToolBarHelper::preferences('com_doska');

		JToolBarHelper::divider();
		JToolbarHelper::publish('categories.publish', 'JTOOLBAR_PUBLISH', true);
		JToolbarHelper::unpublish('categories.unpublish', 'JTOOLBAR_UNPUBLISH', true);
		    
	    JToolBarHelper::deleteList('', 'categories.delete', 'JTOOLBAR_DELETE'); 
	}
	
	protected function setDocument() {
		
		$document = JFactory::getDocument();
		$document->addStyleSheet(JUri::root(TRUE)."/media/com_doska/css/style.css");
		
		/*$document->setTitle(JText::_('COM_DOSKA_ADMINISTRATION'));*/
	}
	
	
	
	
}